# -*- coding: utf-8 -*-
import tkinter
from tkinter import ttk
import win32print

from zebra import Zebra

# windows下用python3和win32print获取本地打印机名称列表
# win32print pyton3自带,无需另外安装
printer_list = []
for it in win32print.EnumPrinters(2):
    # print(it)
    printer_list.append(it[2])
print(printer_list)

# 获取默认打印机名字
DefaultPrinter = win32print.GetDefaultPrinter()
print(DefaultPrinter)

zpl = '^XA^LH200,30^FO20,10^AFN,5,30^FD%1^FS^JMA^LL450^PW700^MD0^PR3^PON^LRN^LH0,0^CI26^FO230,80^BQN,2,5^FDHM,B0200REEL' \
      '\n%2\nPART^FS^LH200,30^FO20,330^AFN,5,1^FD\n%2\n^FS^XZ^'


def send_zpl():
    printer_name = combobox.get().strip()
    print(printer_name)
    z = Zebra(printer_name)
    commands = zpl_text.get(0.0, 'end').strip()
    print(commands)
    z.output(commands)


win = tkinter.Tk()
win.geometry('500x300')
win.title('ZPL调试工具')

text_label = tkinter.Label(win, text='下方输入框输入ZPL指令:')
text_label.place(x=20, y=20)
zpl_text = tkinter.Text(win)
zpl_text.place(x=20, y=40, relwidth=0.9, height=150)
zpl_text.insert(0.0, zpl)

printer_label = tkinter.Label(win, text='选择打印机:')
printer_label.place(x=20, y=220)
combobox = ttk.Combobox(win, values=printer_list, width=40)
combobox.place(x=20, y=240)
combobox.set(DefaultPrinter)

send_button = tkinter.Button(win, text='Send ZPL to printer', command=send_zpl, bg='skyblue', width=23, height=2)
send_button.place(x=300, y=220)

win.mainloop()

'''
一段完整的ZPL指令
^XA
^XA^LH0,0^LL100^PW580
 
^FX 文本
^FO10,10^AFN,0,20,10^FD123456^FS
^FX 一维码
^FO10,10^BY2,11.0,10,40^BCC,90,N^FD12345678^FS
^FX 二维码
^FO10,10^BQ,2,5^CI26^FH^FDQA^FD1215445244^FS
 
^XZ
'''
